/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * This file is part of JMail                                              *
 * Copyright (C) 2002-2003 Yvan Norsa <norsay@wanadoo.fr>                  *
 *                                                                         *
 * JMail is free software; you can redistribute it and/or modify           *
 * it under the terms of the GNU General Public License as published by    *
 * the Free Software Foundation; either version 2 of the License, or       *
 * any later version.                                                      *
 *                                                                         *
 * JMail is distributed in the hope that it will be useful,                *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 * GNU General Public License for more details.                            *
 *                                                                         *
 * You should have received a copy of the GNU General Public License along *
 * with JMail; if not, write to the Free Software Foundation, Inc.,        *
 * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA                 *
 *                                                                         *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.swing.*;
import javax.swing.tree.*;

/** Dialog allowing the user to define a filter */
final class CreateFilter extends JDialog {
	/** Language resource */
	private ResourceBundle msgBundle;

	private JPanel panel;

	private JLabel nameLabel;
	private JTextField nameField;

	private String conditions[];
	private JLabel conditionLabel;
	private JComboBox conditionBox;

	private JLabel triggerLabel;
	private JTextField triggerField;

	private String actions[];
	private JLabel actionLabel;
	private JComboBox actionBox;

	private JLabel targetLabel;
	private JTree targetTree;
	private JScrollPane targetPane;

	private JButton ok;
	private JButton cancel;

	private Profile profile;

	private String name;
	private Filter filter;

	private CreateFilterListener listener;

	private Store store;

	/**
	 * Constructor
	 * 
	 * @param store
	 *            link to the mail account
	 * @param profile
	 *            user profile
	 * @param msgBundle
	 *            language resource
	 */
	protected CreateFilter(Store store, Profile profile,
			ResourceBundle msgBundle) {
		super((JFrame) null, msgBundle.getString("CreateFilter.frameTitle"),
				true);

		this.msgBundle = msgBundle;

		this.store = store;

		name = new String();

		this.profile = profile;

		listener = new CreateFilterListener();

		panel = new JPanel();

		GridBagLayout gridBag = new GridBagLayout();
		GridBagConstraints constraints = new GridBagConstraints();

		panel.setLayout(gridBag);

		Common.buildConstraints(constraints, 1, 0, 2, 1, 30, 10);
		nameLabel = new JLabel(
				msgBundle.getString("CreateFilter.nameLabelText"));
		gridBag.setConstraints(nameLabel, constraints);
		panel.add(nameLabel);

		Common.buildConstraints(constraints, 1, 1, 2, 1, 0, 10);
		nameField = new JTextField(20);
		gridBag.setConstraints(nameField, constraints);
		panel.add(nameField);

		Common.buildConstraints(constraints, 0, 2, 1, 1, 20, 0);
		conditionLabel = new JLabel(
				msgBundle.getString("CreateFilter.conditionLabelText"));
		gridBag.setConstraints(conditionLabel, constraints);
		panel.add(conditionLabel);

		Common.buildConstraints(constraints, 0, 3, 1, 1, 0, 0);
		conditions = new String[3];
		conditions[0] = "From";
		conditions[1] = "Subject";
		conditions[2] = "Dest";

		conditionBox = new JComboBox(conditions);
		gridBag.setConstraints(conditionBox, constraints);
		panel.add(conditionBox);

		Common.buildConstraints(constraints, 1, 2, 1, 1, 20, 0);
		triggerLabel = new JLabel(
				msgBundle.getString("CreateFilter.triggerLabelText"));
		gridBag.setConstraints(triggerLabel, constraints);
		panel.add(triggerLabel);

		Common.buildConstraints(constraints, 1, 3, 1, 1, 0, 0);
		triggerField = new JTextField(20);
		gridBag.setConstraints(triggerField, constraints);
		panel.add(triggerField);

		Common.buildConstraints(constraints, 2, 2, 1, 1, 20, 0);
		actionLabel = new JLabel("Action :");
		gridBag.setConstraints(actionLabel, constraints);
		panel.add(actionLabel);

		Common.buildConstraints(constraints, 2, 3, 1, 1, 0, 0);
		actions = new String[3];
		actions[0] = "Copy";
		actions[1] = "Move";
		actions[2] = "Delete";

		actionBox = new JComboBox(actions);
		actionBox.addActionListener(listener);
		gridBag.setConstraints(actionBox, constraints);
		panel.add(actionBox);

		Common.buildConstraints(constraints, 3, 2, 1, 1, 20, 15);
		targetLabel = new JLabel(
				msgBundle.getString("CreateFilter.targetLabelText"));
		gridBag.setConstraints(targetLabel, constraints);
		panel.add(targetLabel);

		Common.buildConstraints(constraints, 3, 3, 1, 1, 0, 0);

		if (store != null)
			targetTree = MailClient.getFolders(store, profile);

		else {
			DefaultMutableTreeNode top = new DefaultMutableTreeNode();
			top.add(new DefaultMutableTreeNode());
			targetTree = new JTree(top);
		}

		targetTree.setRootVisible(false);
		targetTree.getSelectionModel().setSelectionMode(
				TreeSelectionModel.SINGLE_TREE_SELECTION);
		targetTree.putClientProperty("JTree.lineStyle", "Angled");

		int nb = targetTree.getRowCount();

		for (int i = 0; i < nb; i++)
			targetTree.expandRow(i);

		targetPane = new JScrollPane(targetTree);
		gridBag.setConstraints(targetPane, constraints);
		panel.add(targetPane);

		Common.buildConstraints(constraints, 1, 5, 1, 1, 0, 100);
		ok = new JButton("OK");
		ok.addActionListener(listener);
		gridBag.setConstraints(ok, constraints);
		panel.add(ok);

		Common.buildConstraints(constraints, 2, 5, 1, 1, 0, 100);
		cancel = new JButton(msgBundle.getString("CreateFilter.cancelLabel"));
		cancel.addActionListener(listener);
		gridBag.setConstraints(cancel, constraints);
		panel.add(cancel);

		setContentPane(panel);
		setSize(725, 515);
		pack();
		show();
	}

	/**
	 * Return the name of the filter
	 * 
	 * @return filter name
	 */

	/**
	 * Returns the whole filter
	 * 
	 * @return filter object
	 */
	protected final Filter getFilter() {
		return (filter);
	}

	/**
	 * Enables or disables the target's pane
	 * 
	 * @param b
	 *            tells wether it has to be disabled or enabled
	 */
	protected void setTargetEnabled(boolean b) {
		targetLabel.setEnabled(b);
		targetTree.setEnabled(b);
		targetPane.setEnabled(b);
	}

	/** Listener for this class */
	private final class CreateFilterListener implements ActionListener {
		/**
		 * This method is invoked when an event is triggered
		 * 
		 * @param e
		 *            event
		 */
		public final void actionPerformed(ActionEvent e) {
			JComponent c = (JComponent) e.getSource();

			if (c == actionBox) {
				String choice = (String) actionBox.getSelectedItem();

				if (choice.compareTo("Delete") == 0)
					setTargetEnabled(false);

				else
					setTargetEnabled(true);
			}

			else if (c == ok) {
				name = nameField.getText();

				if (name.compareTo("") == 0) {
					JOptionPane.showMessageDialog(null, msgBundle
							.getString("CreateFilter.noNameWarningLabel"),
							"CreateFilter", JOptionPane.WARNING_MESSAGE);
					return;
				}

				String condition = (String) conditionBox.getSelectedItem();

				if (condition == null) {
					JOptionPane.showMessageDialog(null, msgBundle
							.getString("CreateFilter.noConditionWarningLabel"),
							"CreateFilter", JOptionPane.WARNING_MESSAGE);
					return;
				}

				String trigger = triggerField.getText();

				if (trigger.compareTo("") == 0) {
					JOptionPane.showMessageDialog(null, msgBundle
							.getString("CreateFilter.noTriggerWarningLabel"),
							"CreateFilter", JOptionPane.WARNING_MESSAGE);
					return;
				}

				String action = (String) actionBox.getSelectedItem();

				if (action == null) {
					JOptionPane.showMessageDialog(null, msgBundle
							.getString("CreateFilter.noActionWarningLabel"),
							"CreateFilter", JOptionPane.WARNING_MESSAGE);
					return;
				}

				String target = new String();

				String choice = (String) actionBox.getSelectedItem();

				if (choice.compareTo("Delete") != 0) {
					DefaultMutableTreeNode n = (DefaultMutableTreeNode) targetTree
							.getLastSelectedPathComponent();

					if (n == null) {
						JOptionPane
								.showMessageDialog(
										null,
										msgBundle
												.getString("CreateFilter.noTargetWarningLabel"),
										"CreateFilter",
										JOptionPane.WARNING_MESSAGE);
						return;
					}

					target = n.toString();
				}

				filter = new Filter(name, condition, trigger, action, target,
						false);

				hide();
			}

			else if (c == cancel) {
				filter = null;
				hide();
			}
		}
	}
}
